package org.example.gochannel;

public class ChannelTest {
    public static void test1() {
        JavaGoChannel<Integer> channel = JavaGoChannel.Make();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        Integer out = channel.out();
                        System.out.println("get " + out);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        Thread.sleep(1000);
                        channel.in(1);
                        System.out.println("-------------------------");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        }).start();
    }

    public static void test2() throws InterruptedException {
        JavaGoChannel<Integer> channel = JavaGoChannel.Make();
        JavaGoChannel<Integer> channelasdasdsa = JavaGoChannel.Make();
        ChannelExecWrapper<Integer> wrapper1 = new ChannelExecWrapper<Integer>(channel) {
            @Override
            protected void exec(Integer para) {
                System.out.println("1 get " + para);
            }
        };

        ChannelExecWrapper<Integer> wrapper2 = new ChannelExecWrapper<Integer>(channelasdasdsa) {
            @Override
            protected void exec(Integer para) {
                System.out.println("2 get " + para);
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                for (; ; ) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    channel.in(count++);
                    channelasdasdsa.in(count++);
                }
            }
        }).start();

        for (; ; ) {
            SelectChGroup.watchAndExecOnce(wrapper1, wrapper2);
        }
    }

    public static void test3() throws InterruptedException {
        JavaGoChannel<Integer> channel = JavaGoChannel.Make();
        TimeChannelWrapper timeWrapper1 = new TimeChannelWrapper(channel, 1000) {
            @Override
            protected void exec(Integer para) {
                System.out.println(System.currentTimeMillis() + " timer get " + para);
            }
        };

        JavaGoChannel<Integer> channelasdasdsa = JavaGoChannel.Make();
        ChannelExecWrapper<Integer> wrapper12312 = new ChannelExecWrapper<Integer>(channelasdasdsa) {
            @Override
            protected void exec(Integer para) {
                System.out.println("1 get " + para);
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        Thread.sleep(1000);
                        timeWrapper1.resetTimer(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
            }
        }).start();

        for (; ; ) {
            SelectChGroup.watchAndExecOnce(timeWrapper1, wrapper12312);
        }
    }


    public static void main(String[] args) throws InterruptedException {
        test3();
    }
}
